package cn.action;


import cn.domain.Privilege;
import cn.service.PrivilegeService;
import javafx.application.Application;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * Created by Administrator on 2017/3/29 0029.
 */
public class PrivilegeListent implements ServletContextListener {

    /**
     * 初始化方法
     * @param servletContextEvent
     */
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //1、获取spring容器
        WebApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
        //2从spring容器中获取privilegeService
        PrivilegeService privilege= (PrivilegeService) applicationContext.getBean("privilegeServiceImpl");
        //3使用service查询全线数据
        List<Privilege> list=privilege.privilegeTop();
        //4将全线数据放入application作用域
        applicationContext.getServletContext().setAttribute("list",list);
        System.out.println(list);
    }

    /**
     * 销毁方法
     * @param servletContextEvent
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
